package novemberBot;

import novemberBot.controller.Localizer;
import novemberBot.mapmaking.PathFinder;
import novemberBot.navigation.Navigator;
import novemberBot.sensor.LineChecker;

public class Emergency extends Thread{
	
	LineChecker left;
	LineChecker right;
	boolean line;
	Localizer loc;
	Navigator theCaptain;
	PathFinder theOvermind;
	public Emergency(Localizer loc, PathFinder theOvermind){
		this.loc=loc;
		this.theOvermind=theOvermind;
		left= FactoryFloorRobot.getInstance().getLeftChecker();
		right= FactoryFloorRobot.getInstance().getRightChecker();
		theCaptain=FactoryFloorRobot.getInstance().getNavigator();
		this.start();
	}
	
	@SuppressWarnings("deprecation")
	public void run(){
		while(true){
			left.setEnable(true);
			right.setEnable(true);
			long wait=System.currentTimeMillis();
			while(System.currentTimeMillis()-wait<30000){
				if(left.onLine())
					line=true;
				
				if(right.onLine())
					line=true;
			}
			if(!line){
				FactoryFloorRobot.getInstance().stop();
				try {
					theCaptain.sleep(600000);
				} catch (InterruptedException e) {
				}
				theCaptain=new Navigator();
				FactoryFloorRobot.getInstance().stop();
				FactoryFloorRobot.getInstance().setSpeeds(-6, 0);
				wait =System.currentTimeMillis();
				while((!left.onLine()||!right.onLine()) && System.currentTimeMillis()-wait<5000)Thread.yield();
				FactoryFloorRobot.getInstance().stop();
				loc.lightLocalize();
				theCaptain.setPath(theOvermind.makePathTo(FactoryFloorRobot.DROP_OFF), true);
				
			}else{
				line=false;
			}
			
		}
	}

}
